
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QPushButton,  QWidget,  QFrame
from PyQt4.QtCore import pyqtSignature

from Resources import cards_rc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class CardView(QPushButton):
	
	# CARD STATE
	STATE_CLOSE		=	300
	STATE_OPEN		=	301
	
	# CARD TYPE 
	TYPE_CLICKABLE		=	400
	TYPE_NOTCLICKABLE	=	401
	
	# CARD SIZE
	CARD_WIDTH		=	50
	CARD_HEIGHT		=	75
	
	def __init__(self, cardID, cardType = TYPE_CLICKABLE,  parent = None):
		QPushButton.__init__(self,  parent)
		
		# init a new card view
		sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
		sizePolicy.setHorizontalStretch(0)
		sizePolicy.setVerticalStretch(0)
		sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
		self.setSizePolicy(sizePolicy)
		
		self.setCursor(QtCore.Qt.PointingHandCursor)
		
		self.setFlat(True)
		
		self.cardID = cardID
		self.cardType = cardType
		
		# default position
		self.position = QtCore.QPoint(0,  0)
		self.setGeometry(0,  0,  0,  0)
		
		# default is open card
		self.cardState = CardView.STATE_OPEN
		
		self.setIconSize(QtCore.QSize(self.CARD_WIDTH,  self.CARD_HEIGHT))
		
	def drawCard(self):
		icon = QtGui.QIcon()
		
		if self.cardState == CardView.STATE_CLOSE :
			icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/cards/cards/back.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
		elif self.cardState == CardView.STATE_OPEN :
			icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/cards/cards/"+ str(self.cardID) +".png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
		self.setIcon(icon)
		self.setGeometry(QtCore.QRect(self.position, QtCore.QSize(CardView.CARD_WIDTH,  CardView.CARD_HEIGHT)))
		if self.cardType == CardView.TYPE_CLICKABLE :
			self.clicked.connect(self.onClicked)
		self.show()
		
	def onClicked(self):
		if self.cardType == CardView.TYPE_CLICKABLE :
			from GameController.GameController import Controller
			controller = Controller()
			controller.handleEvent(self.cardID)
		


	
class DeckView(QFrame):
	
	# POSITION
	FRAME_RECT	=	QtCore.QRect(442, 270, 70, 75)
	
	# STATE
	STATE_NORMAL	=	100
	STATE_SPECIAL	=	101
	STATE_NONE	=	102
	
	def __init__(self,  specialCard,  parent = None):
		QFrame.__init__(self,  parent)

		self.setGeometry(DeckView.FRAME_RECT)
		self.setFrameShape(QtGui.QFrame.NoFrame)
		
		# add special card
		self.specialCardView = CardView(specialCard.GetID(), 0, self)
		self.specialCardView.position = QtCore.QPoint(0, 0)
		self.specialCardView.drawCard()
		
		# add deck card
		self.deckCardView = CardView(-1, CardView.TYPE_CLICKABLE, self)
		self.deckCardView.position = QtCore.QPoint(20, 0)
		self.deckCardView.cardState = CardView.STATE_CLOSE
		self.deckCardView.drawCard()
		
		self.setObjectName("deckCardView")
		
		self.show()
		
	def updateState(self,  state):
		if state == DeckView.STATE_NORMAL:
			self.specialCardView.show()
			self.deckCardView.show()
		elif state == DeckView.STATE_SPECIAL:
			self.specialCardView.show()
			self.deckCardView.hide()
		elif state == DeckView.STATE_NONE:
			self.specialCardView.hide()
			self.deckCardView.hide()


class CardsView(QWidget):
	
	# LAYOUT
	BOTTOM_LAYOUT		= 	QtCore.QRect(130, 490, 691, 160)
	TOP_LAYOUT		= 	QtCore.QRect(130, 10, 691, 160)
	
	
	def __init__(self, arrayCard, parent = None, player = 0):
		QWidget.__init__(self,  parent)
		
		self.player = player
		
		if player == 0 :
			self.setGeometry(CardsView.BOTTOM_LAYOUT)
			self.cardsType = CardView.TYPE_CLICKABLE
		else :
			self.setGeometry(CardsView.TOP_LAYOUT)
			self.cardsType = CardView.TYPE_NOTCLICKABLE
		
		self.arrayCard = arrayCard
			
		self.layout = QtGui.QHBoxLayout(self)
		self.layout.setSpacing(0)
		self.layout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
		spacerItem = QtGui.QSpacerItem(13, 13, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
		self.layout.addItem(spacerItem)
		
		self.frames = []
	
		for card in arrayCard:
			# render a frame
			frame = QtGui.QFrame(self)
			frame.setMaximumSize(QtCore.QSize(50, 160))
			frame.setFrameShape(QtGui.QFrame.NoFrame)
			
			y = 0
			
			frame.cardsView = []
			
			for suit in card:
				cardView = CardView(suit.GetID(), self.cardsType, frame)
				cardView.position = QtCore.QPoint(0, y)
				cardView.drawCard()
				
				frame.cardsView.append(cardView)
				y += 20
				
			self.layout.addWidget(frame)
			
			self.frames.append(frame)
		
		self.layout.addItem(spacerItem)
		self.show()
		
	def pushCard(self, absPos, pushType = 'ATTACK', playingAreaPosition = 'RIGHT'):
		from GameController.GameController import Controller
		controller = Controller()
		
		frameIndex = absPos[0]
		cardIndex = absPos[1]
		frame = self.frames[frameIndex]
		cardView = frame.cardsView[cardIndex]
		frame.cardsView.remove(cardView)
		if len(frame.cardsView) == 0:
			self.frames.remove(frame)
		
		if playingAreaPosition == 'LEFT':
			playingFrame = controller.window.playingFrameLeft
			if self.player == 0:
				cardX = CardView.CARD_WIDTH + 30
			else:
				cardX = 0
		elif playingAreaPosition == 'RIGHT' :
			playingFrame = controller.window.playingFrameRight
			if self.player == 0:
				cardX = 0
			else:
				cardX = CardView.CARD_WIDTH + 30
			
		cardView.setParent(playingFrame)
		cardView.setGeometry(cardX, playingFrame.nextY, CardView.CARD_WIDTH, CardView.CARD_HEIGHT)
		cardView.cardType = CardView.TYPE_NOTCLICKABLE
		cardView.show()
		
		if pushType == 'DEFEND':
			playingFrame.nextY += 20
		
	def botPushCard(self, cardID, pushType = 'ATTACK', playingAreaPosition = 'RIGHT'):
		from GameController.GameController import Controller
		controller = Controller()
		
		if playingAreaPosition == 'LEFT':
			playingFrame = controller.window.playingFrameLeft
			cardX = 0
		elif playingAreaPosition == 'RIGHT' :
			playingFrame = controller.window.playingFrameRight
			cardX = CardView.CARD_WIDTH + 30
			
		cardView = CardView(cardID, CardView.TYPE_NOTCLICKABLE,  playingFrame)
		cardView.position = QtCore.QPoint(cardX, playingFrame.nextY);
		cardView.drawCard()
		
		if pushType == 'DEFEND':
			playingFrame.nextY += 20
			
	def takeCards(self, arrayCard):
		self.arrayCard = arrayCard
		
		self.clearLayout()
		
		spacerItem = QtGui.QSpacerItem(13, 13, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
		self.layout.addItem(spacerItem)
		
		self.frames = None
		self.frames = []
	
		for card in arrayCard:
			# render a frame
			frame = QtGui.QFrame(self)
			frame.setMaximumSize(QtCore.QSize(50, 160))
			frame.setFrameShape(QtGui.QFrame.NoFrame)
			
			y = 0
			
			frame.cardsView = []
			
			for suit in card:
				cardView = CardView(suit.GetID(), self.cardsType, frame)
				cardView.position = QtCore.QPoint(0, y)
				cardView.drawCard()
				
				frame.cardsView.append(cardView)
				y += 20
				
			self.layout.addWidget(frame)
			
			self.frames.append(frame)
		self.layout.addItem(spacerItem)
		self.show()
		
	def clearLayout(self):
		layout = self.layout
		for frame in self.frames:
			frame.deleteLater()

class SessionView(QFrame):
	# POSITION
	RIGHT_FRAME_RECT	=	QtCore.QRect(661, 171, 159, 299)
	LEFT_FRAME_RECT	=	QtCore.QRect(131, 171, 159, 299)
	
	def __init__(self, side, parent = None):
		QFrame.__init__(self,  parent)
		
